// Tab Content script v2.0- © Dynamic Drive DHTML code library (http://www.dynamicdrive.com)

function ddtabcontent(tabinterfaceid) {
    this.ddtabcontent(tabinterfaceid, 1)
}

function ddtabcontent(tabinterfaceid, idx) {
    this.tabinterfaceid =   tabinterfaceid //ID of Tab Menu main container
    this.tabs           =   document.getElementById(tabinterfaceid).getElementsByTagName("a") //Get all tab links within container
    this.subcontentids=[] //Array to store ids of the sub contents ("rel" attr values)
    this.selectedClassTarget="link" //keyword to indicate which target element to assign "selected" CSS class ("linkparent" or "link")
    this.selectedtab = idx
}

ddtabcontent.prototype={


    setselectedClassTarget:function(objstr){ //PUBLIC function to set which target element to assign "selected" CSS class ("linkparent" or "link")
        this.selectedClassTarget=objstr || "link"
    },

    getselectedClassTarget:function(tabref){ //Returns target element to assign "selected" CSS class to
        return (this.selectedClassTarget==("linkparent".toLowerCase()))? tabref.parentNode : tabref
    },

    expandtab:function(tabref){
        var subcontentid=tabref.getAttribute("rel") //Get id of subcontent to expand
        this.expandsubcontent(subcontentid)
        for (var i=0; i<this.tabs.length; i++){ //Loop through all tabs, and assign only the selected tab the CSS class "selected"
            this.getselectedClassTarget(this.tabs[i]).className=(this.tabs[i].getAttribute("rel")==subcontentid)? "selected" : ""
        }
    },

    expandsubcontent:function(subcontentid){
        for (var i = 0; i < this.subcontentids.length; i++){
            var subcontent = document.getElementById(this.subcontentids[i]) //cache current subcontent obj (in for loop)
            subcontent.style.display = (subcontent.id==subcontentid)? "block" : "none" //"show" or hide sub content based on matching id attr value
        }
    },

    init:function(){
        //var selectedtab =   -1 //Currently selected tab index (-1 meaning none)
        for (var i=0; i < this.tabs.length; i++){
            this.tabs[i].tabposition=i //remember position of tab relative to its peers
            if (this.tabs[i].getAttribute("rel")){
                var tabinstance=this
                this.subcontentids[this.subcontentids.length]=this.tabs[i].getAttribute("rel") //store id of sub content ("rel" attr value)
                this.tabs[i].onclick=function(){
                    tabinstance.expandtab(this)
                    return false
                }
                if (this.selectedtab==-1 || this.selectedtab==-1 && this.getselectedClassTarget(this.tabs[i]).className=="selected"){
                    this.selectedtab=i //Selected tab index, if found
                }
            }
        } 
        if (this.selectedtab!=-1) //if a valid default selected tab index is found
            this.expandtab(this.tabs[this.selectedtab]) //expand selected tab (either from URL parameter or class="selected" class)
        
        
    } 

} 

